#ifndef FTPCLIENT_H
#define FTPCLIENT_H

#include <QObject>
#include <QHash>
#include <QTemporaryDir>
#include <QIODevice>
#include "ftpclient_global.h"
#include "qurlinfo.h"
class QFtp;

typedef struct FTP_ARGS{
    QString cmd;
    QString local;
    QString remote;
    QIODevice *dev;
    FTP_ARGS(): dev(0){}
    ~FTP_ARGS() { if (dev) dev->deleteLater(); }
} FTP_ARGS;

class FTPCLIENTSHARED_EXPORT FtpClient : public QObject
{
    Q_OBJECT
public:
    explicit FtpClient(QObject *parent = 0);
    ~FtpClient();

    QString getTempPath() const { return tempDir.path(); }
    void setNameFilter(const QStringList &nameFilter) { filter = nameFilter; }

signals:
    void downloaded(const QString &file);
    void uploaded(const QString &file);

public slots:
    void put(const QString &file, const QString &path);
    void get(const QString &path);
    void get(const QStringList &paths);
    void getAll(const QString &dir);

    void connectFtp();

private slots:
    void onStateChanged(int state);
    void onListInfo(const QUrlInfo &url);
    void onCmdFinished(int id, bool error, const QString &err);

private:
    QFtp *ftp;
    QStringList filter;
    QTemporaryDir tempDir;
    QHash<QString, QString> getHash;
    QHash<int, FTP_ARGS*> cmdHash;
    QList<QUrlInfo> urls;
};

#endif // FTPCLIENT_H
